package com.wt.struggle.goodbye.user.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.CommentListActivity
import com.wt.struggle.goodbye.main.activity.ShareActivity
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import kotlinx.android.synthetic.main.about_invitation_layout.*
import kotlinx.android.synthetic.main.head.*

/**
 * 相关帖子
 */
class AboutInvitationActivity : ProActivity() {
    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.GET_MY_TIE -> {
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )
                        showAdapter(arr)
                    }
                }
            }

            Send.SEARCH_LIST -> {
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val ll = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )
                        showAdapter(ll)
                    }
                }
            }
        }

    }

    private fun showAdapter(arr: ArrayList<NewsInfo>?) {
        adapter.updateDataClear(arr!!)

    }

    val tabList = arrayListOf<String>("我的帖子", "我的回复")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.about_invitation_layout)
        tvTitle.text = "相关帖子"

        imageBack.setOnClickListener {
            finish()
        }

        initAdapter()

        initTabLayout()

        send!!.getSearchList(page, 10, 0, 0, uid, "", null, handler!!)

    }


    var page = 1

    /**
     * 获取我的帖子信息
     */
    private fun getMyTieList() {
        send!!.getMyTieList(uid, page, handler)

    }

    var tabPosition: Int = 0

    private fun initTabLayout() {

        for (i in tabList.indices) {

            val textView = TextView(this)
            textView.gravity = Gravity.CENTER
            textView.text = tabList[i]
            changeTextStyle(textView, i == 0)

            invitationTabLayout.addTab(invitationTabLayout.newTab().setCustomView(textView))
        }

        invitationTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                for (i in tabList.indices) {
                    val textView = invitationTabLayout.getTabAt(i)!!.customView as TextView
                    changeTextStyle(textView, i == tab!!.position)
                }

                tabPosition = tab!!.position

                when (tab.position) {
                    0 -> send!!.getSearchList(page, 10, 0, 0, uid, "", null, handler!!)
                    1 -> getMyTieList()
                }
            }

        })
    }

    lateinit var adapter: PickUserAdapter
    val list = arrayListOf<NewsInfo>()

    private fun initAdapter() {
        contentRecyclerView.layoutManager = LinearLayoutManager(this)
        contentRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = PickUserAdapter(this, list)
        adapter.isEnterCode = 1
        contentRecyclerView.adapter = adapter

        adapter.onFocusOnClickListener = object : PickUserAdapter.OnFocusOnClickListener {
            override fun onMore(position: Int) {
                openShare(position)
            }

            override fun onCollect(position: Int) {

            }

            override fun onZan(position: Int) {

            }

            override fun onDownZan(position: Int) {
                val intent = Intent(this@AboutInvitationActivity, SiXinOrRecoverActivity::class.java)
                intent.putExtra("code", 1)
                intent.putExtra("user", list[position].userExtraInfo)
                startActivity(intent)
            }

            override fun onComment(position: Int) {
                val intent = Intent(this@AboutInvitationActivity, CommentListActivity::class.java)
                val info = list[position]
                intent.putExtra("info", info)

                intent.putParcelableArrayListExtra("vvvvList", info.videoList)
                intent.putParcelableArrayListExtra("audioList", info.audioList)

                startActivity(intent)

            }

            override fun onFocusOn(position: Int) {

            }

        }
    }

    var clickPosition: Int = 0

    fun openShare(position: Int) {

        val intent = Intent(this, ShareActivity::class.java)

        val info = list[position]
        intent.putExtra("info", info)
        intent.putParcelableArrayListExtra("videoList", info.videoList)
        intent.putParcelableArrayListExtra("voiceList", info.audioList)
        intent.putExtra(
            "code", if (tabPosition == 0) {
                2
            } else {
                4
            }
        )

        startActivityForResult(intent, 1234)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                1234 -> {
                    list.removeAt(clickPosition)
                    adapter.notifyItemRemoved(clickPosition)
                }
            }
        }
    }
}